package com.example.mybatis.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.example.mybatis.bean.*;
import com.example.mybatis.mapper.*;
import com.example.mybatis.service.CatalogueService;
import com.example.mybatis.service.SafetyEducationService;
import com.example.mybatis.utils.ResultUtil;
import com.example.mybatis.utils.validator.DataValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Savepoint;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SafetyEducationImpl implements SafetyEducationService {
    @Resource
    SafetyEducationMapper safetyEducationMapper;
    @Resource
    CatalogueService catalogueService;

    @Resource
    DetailsMapper detailsMapper;
    @Resource
    SafetyStudentInformationMapper safetyStudentInformationMapper;
    @Resource
    StudentInformationMapper studentInformationMapper;
    @Resource
    SafetyDetailsMapper safetyDetailsMapper;
    @Resource
    StudentInformationDetailsMapper   studentInformationDetailsMapper;

    @Override
    public ResultUtil selectList(String title, Integer catalogueId) {
        return ResultUtil.ok().put("dataList", detailsMapper.selectList1(catalogueId, title))
                .put("data", catalogueService.selectList("").get("dataList"));
    }


    public ResultUtil save(SafetyEducation safetyEducation) {
        //保存安全培训,并返回主键值
        safetyEducationMapper.save(safetyEducation);

        //保存安全培训人员，,并返回主键值
        SafetyStudentInformation safetyStudentInformation = new SafetyStudentInformation();
        safetyStudentInformation.setSafetyEducationId(String.valueOf(safetyEducation.getId()));
        safetyStudentInformation.setStudentInformationId(safetyEducation.getStudentInformationId());
        safetyStudentInformation.setFlag("0");
        safetyStudentInformationMapper.insert(safetyStudentInformation);

        //保存课程
        List<Integer> detailsIds = safetyEducation.getDetailsIds();
        if (!detailsIds.isEmpty()) {
            List<SafetyDetails> safetyDetailsList = detailsIds.stream().map(detailsId -> {
                SafetyDetails safetyDetails = new SafetyDetails();
                safetyDetails.setId(IdUtil.simpleUUID());
                safetyDetails.setSafetyEducationId(String.valueOf(safetyEducation.getId()));
                safetyDetails.setDetailsId(String.valueOf(detailsId));
                return safetyDetails;
            }).collect(Collectors.toList());
            //保存安全培训下课程

            safetyDetailsMapper.insertBatch(safetyDetailsList);

            //保存安全培训下人员的所有课程
            List<StudentInformationDetails> studentInformationDetailsList = safetyDetailsList.stream().map(safetyDetails -> {
                StudentInformationDetails studentInformationDetails = new StudentInformationDetails();
                studentInformationDetails.setId(IdUtil.simpleUUID());
                studentInformationDetails.setSafetyStudentInformationId(safetyStudentInformation.getId());
                studentInformationDetails.setSafetyDetailsId(safetyDetails.getId());
                return studentInformationDetails;

            }).collect(Collectors.toList());
            studentInformationDetailsMapper.insertBatch(studentInformationDetailsList);
        }

        return ResultUtil.ok();

    }

    @Override
    public ResultUtil selectAll(String learningStyle) {
        List<SafetyEducation> dataList = safetyEducationMapper.selectAll(learningStyle);
        return ResultUtil.ok().put("dataList", dataList);
    }


    public ResultUtil selectList1(String name, String status) {
        return ResultUtil.ok().put("dataList", studentInformationMapper.selectList( name,status));

    }

    @Override
    public ResultUtil selectFinishPercent(Date month) {
        return ResultUtil.ok()
                .put("dataList",safetyEducationMapper.selectFinishPercent(month))
                .put("totalRate",safetyEducationMapper.selectCountPercent());

    }

    @Override
    public ResultUtil selectDetail( Integer detailsId) {
        return ResultUtil.ok().put("dataList", safetyEducationMapper.selectDetail( detailsId));

    }




    public ResultUtil selectStudent(Integer studentInformationId) {
        return ResultUtil.ok().put("dataList", safetyEducationMapper.selectStudent(studentInformationId));

    }

    @Override
    public ResultUtil selectGraphData() {
        //获取当前年的所有月份
        List<GraphModel> graphModelList =new ArrayList<>();

        Calendar calendar = Calendar.getInstance();

        int i = 0;
        while ( i < 12 ) {
            //修改月份
            calendar.set(Calendar.MONTH, i);
            GraphModel graphModel = new GraphModel();
            //获取修改后的日期对象，转换成日期字符串
            graphModel.setMonth(DateUtil.format(calendar.getTime(),"yyyy-MM"));
            graphModelList.add(graphModel);
            i++;//月份增加
        }


        List<SafetyEducation> allApplyList = safetyEducationMapper.selectAllApply();

        //遍历数据，对比月份,返回所有的数量
        List<Integer> totalList = graphModelList.stream().map(item->{
            //循环数据库数据
            allApplyList.stream()
                    //对比月份是否相同
                    .filter(item1->item.getMonth().equals(item1.getGainMonth()))
                    .forEach(item1->{
                        //设置当前月份的数量
                        item.setTotal(item1.getAllNumber());
                    });
            //只返回数量
            return item.getTotal();
        }).collect(Collectors.toList());


        List<SafetyEducation> finishMonthList = safetyEducationMapper.selectAchieve();

        //遍历数据，对比月份,返回所有的数量
        List<Integer> finishList = graphModelList.stream().map(item->{
            //循环数据库数据
            finishMonthList.stream()
                    //对比月份是否相同
                    .filter(item1->item.getMonth().equals(item1.getGainMonth()))
                    .forEach(item1->{
                        //设置当前月份的数量
                        item.setTotal(item1.getAllNumber());
                    });
            //只返回数量
            return item.getTotal();
        }).collect(Collectors.toList());

        Integer onLineAll = safetyEducationMapper.selectOnLineAll();

        Integer onLineFinish = safetyEducationMapper.selectOnLineFinish();

        Integer siteAll = safetyEducationMapper.selectSiteAll();

        Integer siteFinish = safetyEducationMapper.selectSiteFinish();

        return ResultUtil.ok()
                .put("totalList",totalList)
                .put("finishList",finishList)
                .put("onLineAll",onLineAll)
                .put("onLineFinish",onLineFinish)
                .put("siteAll",siteAll)
                .put("siteFinish",siteFinish);

    }
}
